package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.OrderFromDto;
import com.example.dto.ProDto;
import com.example.dto.VipDto;
import com.example.mapper.OrderFromMapper;
import com.example.mapper.VipMapper;
import com.example.service.OrderFromService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderFromServiceImpl implements OrderFromService {
    @Autowired
    private OrderFromMapper mapper;
    @Autowired
    private VipMapper vipMapper;

    @Override
    public void add(OrderFromDto dto) {
        mapper.insert(dto);
    }

    @Override
    public IPage<OrderFromDto> select(OrderFromDto dto) {
        QueryWrapper<OrderFromDto> queryWrapper = new QueryWrapper<>();
//        根据订单号
        if (dto.getOrderNumber() != null && !dto.getOrderNumber().isEmpty()) {
            queryWrapper.eq("order_number", dto.getOrderNumber());
        }
//        根据会员id
        if (dto.getVipPhone() != null && dto.getVipPhone() != 0) {
            QueryWrapper<VipDto> qw=new QueryWrapper<>();
            qw.eq("phone",dto.getVipPhone());
            List<VipDto> vipDtos = vipMapper.selectList(qw);
            queryWrapper.eq("vip_id", vipDtos.get(0).getId());

        }
//        根据时间区间
        if (dto.getStartTime() != null && dto.getEndTime() != null) {
            queryWrapper.between("add_time", dto.getStartTime(), dto.getEndTime());
        }
        Page<OrderFromDto> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        IPage<OrderFromDto> result = mapper.selectPage(page, queryWrapper);
//       当会员id不为空时  再放入会员信息
        if (result != null && result.getSize()>0) {
            List<OrderFromDto> list = result.getRecords();
            for (OrderFromDto orderFromDto : list) {
                VipDto vipDto = vipMapper.selectById(orderFromDto.getVipId());
                orderFromDto.setVipPhone(vipDto.getPhone());
                orderFromDto.setVipNickName(vipDto.getNickName());
            }
            result.setRecords(list);
        }

        return result;
    }


    @Override
    public void update(OrderFromDto dto) {
        QueryWrapper<OrderFromDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dto.getId());
        mapper.update(dto,queryWrapper);
    }

    @Override
    public void delete(OrderFromDto dto) {
        QueryWrapper<OrderFromDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dto.getId());
        mapper.delete(queryWrapper);
    }


}
